package cn.cdeden.resource.service.impl;

import cn.cdeden.common.core.utils.StringUtils;
import cn.cdeden.common.mybatis.core.page.PageQuery;
import cn.cdeden.resource.domain.SysOssEs;
import cn.cdeden.resource.esmapper.SysOssEsMapper;
import cn.cdeden.resource.easyes.util.EsDataOperation;
import cn.cdeden.resource.service.OssFileEsService;
import com.alibaba.fastjson2.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.dromara.easyes.core.kernel.EsWrappers;
import org.dromara.easyes.core.toolkit.EntityInfoHelper;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ProjectName : KnowCore
 * @Package : cn.cdeden.resource.es.service.impl
 * @ClassName : SysOssEsServiceImpl
 * @Author : Yu.
 * @Date : 2024/12/10 13:56
 * @Version : 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
@ConditionalOnProperty(value = "easy-es.enable", havingValue = "true")
public class OssFileEsServiceImpl implements OssFileEsService {

    @Autowired
    private SysOssEsMapper ossEsMapper;  //我没有找到添加数据满足 es 索引的场景，所以没有使用
    // 使用了  RestHighLevelClient 去实现 es 管道的数据添加


    /**
     * 自定义 使用es方法
     */
    @Autowired
    private EsDataOperation operation;

    /**
     * 创建 通道名称
     */
    @Override
    public List<SysOssEs> selectList(LambdaEsQueryWrapper<SysOssEs> wrapper) {
        try {
            return ossEsMapper.selectList(wrapper);// 查询
        }catch (Exception e){
            log.error("查询数据异常",e);
        }
        return null;
    }

    @Override
    public List<SysOssEs> selectList(SysOssEs ossEs) {
        LambdaEsQueryWrapper<SysOssEs> wrapper = buildQueryWrapper(ossEs); // 构建查询条件
        try {
            return ossEsMapper.selectList(wrapper);// 查询
        }catch (Exception e){
            log.error("查询数据异常",e);
        }
        return new ArrayList<>();
    }

    @Override
    public EsPageInfo<SysOssEs> selectPage(SysOssEs ossEs, PageQuery pageQuery) {
        LambdaEsQueryWrapper<SysOssEs> wrapper = buildQueryWrapper(ossEs); // 构建查询条件
        EsPageInfo<SysOssEs> sysOssEsEsPageInfo = ossEsMapper.pageQuery(wrapper, pageQuery.getPageNum(), pageQuery.getPageSize());
        return sysOssEsEsPageInfo;
    }


    private LambdaEsQueryWrapper<SysOssEs> buildQueryWrapper1(SysOssEs bo) {
        return new LambdaEsQueryWrapper<>();
    }

    private LambdaEsQueryWrapper<SysOssEs> buildQueryWrapper(SysOssEs bo) {
//        LambdaQueryWrapper<SysOssEs> lqw = Wrappers.lambdaQuery(SysOssEs.class);
        LambdaEsQueryWrapper<SysOssEs> lqw = EsWrappers.lambdaQuery(SysOssEs.class);
        lqw.and(StringUtils.isNotBlank(bo.getTitle()) || StringUtils.isNotBlank(bo.getContent()),
            wrapper -> wrapper.like(StringUtils.isNotBlank(bo.getTitle()),SysOssEs::getTitle, bo.getTitle())
                .or()
                .like(StringUtils.isNotBlank(bo.getContent()),SysOssEs::getContent, bo.getContent())
        );

//        lqw.eq(StringUtils.isNotBlank(bo.getTeamId()), SysOssEs::getTeamId, bo.getTameId());
        lqw.eq(StringUtils.isNotBlank(bo.getModuleType()), SysOssEs::getModuleType, bo.getModuleType());
        lqw.eq(StringUtils.isNotBlank(bo.getTeamId()), SysOssEs::getTeamId, bo.getTeamId());
        lqw.eq(StringUtils.isNotBlank(bo.getOssId()), SysOssEs::getOssId, bo.getOssId());
        lqw.and(
            ObjectUtils.isNotEmpty(bo.getUserIdList()) ||
            ObjectUtils.isNotEmpty(bo.getRoleIdList()) ||
            ObjectUtils.isNotEmpty(bo.getDeptIdList()) ||
            ObjectUtils.isNotEmpty(bo.getPostIdList()),
            x-> {
                x.or().in(ObjectUtils.isNotEmpty(bo.getUserIdList()), SysOssEs::getUserIdList, bo.getUserIdList());
                x.or().in(ObjectUtils.isNotEmpty(bo.getRoleIdList()), SysOssEs::getRoleIdList, bo.getRoleIdList());
                x.or().in(ObjectUtils.isNotEmpty(bo.getDeptIdList()), SysOssEs::getDeptIdList, bo.getDeptIdList());
                x.or().in(ObjectUtils.isNotEmpty(bo.getPostIdList()), SysOssEs::getPostIdList, bo.getPostIdList());
            });
        lqw.orderByDesc(SysOssEs::getCreateTime); // 创建时间降序
        return lqw;
    }

    @Override
    public boolean insert(SysOssEs document) {
        String indexName = EntityInfoHelper.getEntityInfo(SysOssEs.class).getIndexName();
//        ossEsMapper.createIndex()
        boolean b = operation.insert(indexName, JSONObject.from(document), attachment);
        return b ;
//        return ossEsPlusMapper.insertPipeline(document,attachment);
    }

    public boolean updateById(SysOssEs document) {
        String indexName = EntityInfoHelper.getEntityInfo(SysOssEs.class).getIndexName();
        boolean update = operation.update(indexName, JSONObject.from(document), WriteRequest.RefreshPolicy.IMMEDIATE);
        return update;
    }

    @Override
    public boolean deleteById(String id) {
        return ossEsMapper.deleteById(id) > 0;
    }

    @Override
    public boolean deleteByIds(Collection<String> ids) {
        Integer i = ossEsMapper.deleteBatchIds(ids);
        return i > 0;
    }




}
